<?php

namespace App\Http\Controllers\Web\Member;

use App\Http\Controllers\Web\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class CategoryController extends BaseController
{
    protected $api_url = [
        'index' => ['get', 'cate'],
        'show' => ['get', 'cate/%d'],
        'store' => ['post', 'cate'],
        'put' => ['put', ''],
        'delete' => ['delete', ''],
    ];

    protected $api_host;

    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->api_host = config('url.api_web');
    }

    public function index()
    {
        $search_params = $this->request->all(['id','pid','name']);
        $pcates = [];
        //
        if(Redis::exists("categorys")){
            $cateall = Redis::hgetall("categorys");
            if(is_array($cateall)){
                foreach($cateall as $c){
                    $cates[] = json_decode($c,true);
                }
                $cates = sortArrByManyField( $cates,'root_id',SORT_ASC,'pid',SORT_ASC,'sort',SORT_ASC,'id',SORT_ASC);
                foreach($cates as $c){
                    if(isset($search_params['pid'])){
                        if($c['pid'] == $search_params['pid']){
                            $pcates[] =$c;
                        }
                    }
                    
                    if(isset($search_params['id'])){
                        if($c['id'] == $search_params['id']){
                            $pcates[] =$c;
                        }
                    }
                    
                    if(isset($search_params['name'])){
                        if($c['name'] == $search_params['name']){
                            $pcates[] =$c;
                        }
                    }
                   
                }
            }
        }
        if(!$pcates){
            //参数部分
            
            $type =  $this->request->get('type', "");
            $page = $this->request->get('page', 1);
            $params = [
                'search' => search_params($search_params),
                'searchJoin' => 'and',
                'page' => $page,
                'type'=>$type,
            ];
            
            //请求部分
            $ret = http_web($this->api_url['index'])->send($params);
            if( isset($ret['code']) &&  $ret['code'] === 0 ){
                $pcates = $ret['data']['list'];
            } 
        }
      
        return response()->json($pcates);
     
        
    }
    
    public function create()
    {
        
        
    }
    

    public function show($id)
    {
        dd($id);
    }

    public function update($id)
    {

    }
}